package com.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

public class JdbcConfig {

    //基本类型的注入: @Value + Spring的el表达式
    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;


    @Bean(name = "runner")//等价于bean.xml中的id属性
    @Scope("prototype")//多例模式
    public QueryRunner creatQueryRunner(DataSource ds){
        QueryRunner qr = new QueryRunner(ds);
        return qr;
    }
    @Bean(name = "ds")//等价于bean.xml中的id属性
    public DataSource getDataSource(){
        //创建DruidDataSource连接池
        DruidDataSource dataSource = new DruidDataSource();
        //设置参数
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
